package utils;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateUtil {
	public static Date getNowDate() {
		return new Date(System.currentTimeMillis());
	}

	//完整相差天数
	public static int getIntervalDays(Date fDate, Date oDate) {

	       if (null == fDate || null == oDate) {

	           return -1;

	       }

	       long intervalMilli = oDate.getTime() - fDate.getTime();

	       return (int) (intervalMilli / (24 * 60 * 60 * 1000));

	    }

	//天数差，不关心时间 
	public static int daysOfTwo(Date fDate, Date oDate) {

	       Calendar aCalendar = Calendar.getInstance();

	       aCalendar.setTime(fDate);

	       int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);

	       aCalendar.setTime(oDate);

	       int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);

	       return day2 - day1;

	    }
  
  public static Timestamp getValidDay2(String i){  
	  int day = Integer.parseInt(i);
      if (day == -1) day = 36500;      
      return new Timestamp(getDayOutDate(getNowDate(),day).getTime());  
    }
    
	public static String formatter(String formatter, long times) {
		Date date = new Date(times);
		SimpleDateFormat sdf = new SimpleDateFormat(formatter);
		String dd = sdf.format(date);
		return dd;
	}

	public static String formatter(long times) {
		Date date = new Date(times);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dd = sdf.format(date);
		return dd;
	}

	public static Date getYearOutDate(Date date, int year) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.YEAR, year);
		return new Date(cal.getTimeInMillis());
	}
	
	public static Date getMonthOutDate(Date date, int month) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, month);
		return new Date(cal.getTimeInMillis());
	}
	
	public static Date getDayOutDate(Date date, int day) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, day);
		return new Date(cal.getTimeInMillis());
	}
}
